package com.zhangtai.modules.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.constVo.ContractConst;
import com.zhangtai.modules.controller.aouthLogin.LoginController;
import com.zhangtai.modules.controller.aouthLogin.MemberLoginController;
import com.zhangtai.modules.dao.*;
//import com.zhangtai.modules.dao.ssqlDao.JobDaoMssql;
import com.zhangtai.modules.dto.*;
import com.zhangtai.modules.entity.certificate.CertificateEntity;
import com.zhangtai.modules.entity.common.ImgUrlEntity;
import com.zhangtai.modules.entity.common.MailRecordEntity;
import com.zhangtai.modules.entity.contract.ContractMangerEntity;
import com.zhangtai.modules.entity.employee.EmployeeBpmEntity;
import com.zhangtai.modules.entity.employee.EmployeeInfoEntity;
import com.zhangtai.modules.entity.employee.EmployerCompanyEntity;
import com.zhangtai.modules.entity.salary.UserBankCardEntity;
import com.zhangtai.modules.entity.system.AuthorityLogEntity;
import com.zhangtai.modules.service.certificate.CertificateService;
import com.zhangtai.modules.service.contract.ContractMangerService;
import com.zhangtai.modules.service.credential.CredentialInfoService;
import com.zhangtai.modules.service.employee.*;
import com.zhangtai.modules.service.login.CustomBase64;
import com.zhangtai.modules.service.security.FundsRecordService;
import com.zhangtai.modules.service.security.SecurityRecordService;
import com.zhangtai.modules.vo.AttendVo;
import com.zhangtai.modules.vo.LoginMemberVo;
import com.zhangtai.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 员工基础信息接口
 */
@Api(tags="员工基础信息接口")
@RestController
@RequestMapping("employeeBasis")
@Slf4j
public class EmployeeBasisController {

    @Value("${login.loginUrl}")
    private String loginUrl;

    @Autowired
    private EmployeeEducationService employeeEducationService;
    @Autowired
    private EmployessSocietyexperienceService employessSocietyexperienceService;
    @Autowired
    private EmployeeInorgnameService employeeInorgnameService;
    @Autowired
    private FamilyMemberService familyMemberService;
    @Autowired
    private RelativeRecordsService relativeRecordsService;
    @Autowired
    private EmployeeBpmService employeeBpmService;
    @Autowired
    private ContractMangerService contractMangerService;
    @Autowired
    private ProtocolPersonnelDao protocolPersonnelDao;
    @Autowired
    private EmployeeInfoService employeeInfoService;
    @Autowired
    private EmployerCompanyService employerCompanyService;
    @Autowired
    private UserBankCardDao userBankCardDao;
    @Autowired
    private AuthorityLogDao authorityLogDao;
    @Autowired
    private CertificateService certificateService;

    @Autowired
    private ImgUrlDao imgUrlDao;
    @Autowired
    private CredentialInfoService credentialInfoService;
    @Autowired
    private JobController jobController;
    @Autowired
    private ScoreDao scoreDao;
    @Autowired
    private AwardInfoDao awardInfoDao;
    @Autowired
    private SalaryChangeDao salaryChangeDao;

    @Autowired
    private SecurityRecordService securityRecordService;
    @Autowired
    private FundsRecordService fundsRecordService;
    @Autowired
    private MailRecordDao mailRecordDao;
    @Autowired
    private ContractMangerDao contractMangerDao;
//    @Autowired
//    private JobDaoMssql jobDaoMssql;
    @Autowired
    private LoginController loginController;
    @Autowired
    private MemberLoginController memberLoginController;

    /**
     * 分页查询员工教育信息
     */
    @GetMapping("/queryEmployeeEducationPage")
    @ApiOperation(value = "分页查询员工教育信息")
    public R queryEmployeeSecondmentPage(@RequestParam(value = "oid") String oid,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {

        PageResult result = employeeEducationService.queryPage(oid,pageNum,pageSize);
        return R.ok(result);
    }

    /**
     * 分页查询员工工作经历信息
     */
    @GetMapping("/queryEmployeeSocietyexperiencePage")
    @ApiOperation(value = "分页查询员工工作经历信息")
    public R queryEmployeeSocietyexperiencePage(@RequestParam(value = "oid") String oid,
                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {

        PageResult result = employessSocietyexperienceService.queryPage(oid,pageNum,pageSize);
        return R.ok(result);
    }

    /**
     * 分页查询员工党派信息
     */
    @GetMapping("/queryEmployeeInorgnamePage")
    @ApiOperation(value = "分页查询员工党派信息")
    public R queryEmployeeInorgnamePage(@RequestParam(value = "oid") String oid,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {

        PageResult result = employeeInorgnameService.queryPage(oid,pageNum,pageSize);
        return R.ok(result);
    }

    /**
     * 分页查询员工家庭成员信息
     */
    @GetMapping("/queryFamilyMemberPage")
    @ApiOperation(value = "分页查询员工家庭成员信息")
    public R queryFamilyMemberPage(@RequestParam(value = "oid") String oid,
                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {

        PageResult result = familyMemberService.queryPage(oid,pageNum,pageSize);
        return R.ok(result);
    }

    /**
     * 分页查询员工亲属关系报备信息
     */
    @GetMapping("/queryRelativeRecordsPage")
    @ApiOperation(value = "分页查询员工亲属关系报备信息")
    public R queryRelativeRecordsPage(@RequestParam(value = "oid") String oid,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {

        PageResult result = relativeRecordsService.queryPage(oid,pageNum,pageSize);
        return R.ok(result);
    }

    /**
     * 分页查询员工合同信息
     */
    @GetMapping("/queryContractMangerPage")
    @ApiOperation(value = "分页查询员工合同信息")
    public R queryContractMangerPage(@RequestParam(value = "oid") String oid,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {

        PageResult result = contractMangerService.queryPage(oid,pageNum,pageSize);
        return R.ok(result);
    }

    /**
     * 分页查询员工证明信息
     */
    @GetMapping("/queryCertificatePage")
    @ApiOperation(value = "分页查询员工证明信息")
    public R queryCertificatePage(@RequestParam(value = "oid") String oid,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {

        PageResult result = certificateService.queryPage(oid,pageNum,pageSize);
        List<CertificateEntity> certificateEntityList=result.getList();
        List<CertificateDto> certificateDtoList= new ArrayList<>();
        for (CertificateEntity certificateEntity:certificateEntityList) {
            CertificateDto certificateDto = BeanUtilsMabach.doToDto(certificateEntity, CertificateDto.class);
            if (certificateEntity.getJobId() != null)
                certificateDto.setJobName(jobController.getJobName(certificateEntity.getJobId()));
            if (certificateEntity.getOrgId() != null)
                certificateDto.setOrgName(jobController.getOrgNameById(certificateEntity.getOrgId()));
            if (certificateEntity.getUnitId() != null)
                certificateDto.setUnitName(jobController.getOrgNameById(certificateEntity.getUnitId()));
            certificateDtoList.add(certificateDto);
        }
        result.setList(certificateDtoList);
        return R.ok(result);
    }




    /**
     * 分页查询员工协议信息
     */
    @GetMapping("/queryProtocolPersonnelPage")
    @ApiOperation(value = "分页查询员工协议信息")
    public R queryProtocolPersonnelPage(@RequestParam(value = "oid") String oid,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {

        Page<ProtocolDto> page = new Page<ProtocolDto>(pageNum,pageSize);
        List<ProtocolDto> protocolDtoList=  protocolPersonnelDao.getProtocolPersonnelByOid(oid,page);
        for (ProtocolDto protocolDto:protocolDtoList){
            if (protocolDto.getJobId() != null && StringUtils.isEmpty(protocolDto.getJobname()))
                protocolDto.setJobname(jobController.getJobName(protocolDto.getJobId()));
            if (protocolDto.getOrgId() != null && StringUtils.isEmpty(protocolDto.getOrgname()))
                protocolDto.setOrgname(jobController.getOrgNameById(protocolDto.getOrgId()));
            if (protocolDto.getUnitId() != null && StringUtils.isEmpty(protocolDto.getUnitname()))
                protocolDto.setUnitname(jobController.getOrgNameById(protocolDto.getUnitId()));
        }
        page.setRecords(protocolDtoList);
        return  R.ok(new PageResult<ProtocolDto>(page));
    }

    /**
     * 分页查询员工360评价信息
     */
    @GetMapping("/queryScorePage")
    @ApiOperation(value = "分页查询员工360评价信息")
    public R queryScorePage(@RequestParam(value = "oid") String oid,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        Page<ScoreDto> page = new Page<ScoreDto>(pageNum,pageSize);
        List<ScoreDto> scoreDtoList=  scoreDao.getScoreByOidPage(oid,page);
        for (ScoreDto scoreDto:scoreDtoList){
            if(scoreDto.getJobId()!=null) scoreDto.setJobName(jobController.getJobName(scoreDto.getJobId()));
            if(scoreDto.getOrgId()!=null) scoreDto.setOrgName(jobController.getOrgNameById(scoreDto.getOrgId()));
            if(scoreDto.getUnitId()!=null) scoreDto.setUnitName(jobController.getOrgNameById(scoreDto.getUnitId()));
        }
        page.setRecords(scoreDtoList);
        return R.ok(new PageResult<ScoreDto>(page));
    }

    @PostMapping("loginMember")
    @ApiOperation(value = "登录员工自助端接口")
    public R loginMember(@RequestBody @Valid LoginMemberVo loginMemberVo,HttpServletRequest request, HttpServletResponse response) throws Exception {

        //登录前如果有cookieName先清空
        String cookieValue = CookieUtils.getCookieValue(request, "MemberToken");
        if (!com.alibaba.druid.util.StringUtils.isEmpty(cookieValue)){
            memberLoginController.logOut(request,response);
        }
        if(StringUtils.isEmpty(loginMemberVo.getIp())){
            loginMemberVo.setIp(loginUrl);
        }
        String oidAES = loginMemberVo.getOid(); //AES解密
        String oid = AESUtil.aesDecrypt(oidAES);
        EmployeeInfoEntity employeeInfoEntity=  employeeInfoService.getByIdE(Long.valueOf(oid));
        if(ObjectUtils.isEmpty(employeeInfoEntity)){
            return R.error("未找到该员工");
        }

        String phone=  CustomBase64.encryptDES(employeeInfoEntity.getMobile());
        phone=  phone.replace("=", "%3d");
        String content=phone;
        if (!StringUtils.isEmpty(employeeInfoEntity.getUseraccount())){
            content=  CustomBase64.encryptDES(employeeInfoEntity.getUseraccount());
            content=  content.replace("=", "%3d");
        }
        String url=loginMemberVo.getIp()+"/?zf=66J4nVJaFag%3d&zu="+content+"&zt="+phone;
        return R.ok(url);
    }



    @GetMapping("/getEmployeeById/{id}")
    @ApiOperation(value="根据员工id获取详情(从现有库中查询)")
    // @SysLog(value ="测试[{{id}}]备份",operationType = OperationType.SELECT)
    public R<EmployeeInfoDto> findEmployeeByOid(@PathVariable("id") Long id) {
        EmployeeInfoEntity employeeInfoEntity = employeeInfoService.getByIdE(id);
        if (ObjectUtils.isEmpty(employeeInfoEntity)){
            return R.ok();
        }
        Long oid = employeeInfoEntity.getOid();


        EmployeeInfoDto employeeInfoDto = BeanUtilsMabach.doToDto(employeeInfoEntity, EmployeeInfoDto.class);

        getImg(oid, employeeInfoDto); //封装图片

        setParam(employeeInfoEntity, employeeInfoDto); //封装名称

        Long employerCompanyId = employeeInfoEntity.getEmployerCompanyId();
        EmployerCompanyEntity byIdE = employerCompanyService.getByIdE(employerCompanyId);
        if (!ObjectUtils.isEmpty(byIdE)){
            //        封装甲方单位
            employeeInfoDto.setEmployerCompany(byIdE.getName());
        }


//        封装复职员工信息
        Integer isReinstate = employeeInfoEntity.getIsReinstate(); //0:新员工，1：复职员工，2：旧员工,3:离职超过一年
        Integer i=1;
        Integer k=3;
        if (i.equals(isReinstate)||k.equals(isReinstate)){
            EmployeeBpmEntity employeeBpmEntity = employeeBpmService.getByIdE(oid);
            if (!ObjectUtils.isEmpty(employeeBpmEntity)){
                OldEmployeeDto oldEmployeeDto = BeanUtilsMabach.doToDto(employeeBpmEntity, OldEmployeeDto.class);
                employeeInfoDto.setOldEmployeeDto(oldEmployeeDto);
            }
        }
        EmployeeBpmEntity employeeBpmEntity = employeeBpmService.getByIdE(id);
        if (!ObjectUtils.isEmpty(employeeBpmEntity)){
            employeeInfoDto.setIsRemoteSocialSecurity(employeeBpmEntity.getIsRemoteSocialSecurity());
            employeeInfoDto.setSocialSecurityCity(employeeBpmEntity.getSocialSecurityCity());
            employeeInfoDto.setSocialSecurityProvince(employeeBpmEntity.getSocialSecurityProvince());
        }

        return R.ok(employeeInfoDto);
    }


    /**
     * 列表
     */
    @GetMapping("/getUserBankCardInfo")
    @ApiOperation(value="查询员工工资卡信息")
    public R getUserBankCardInfo( @RequestParam(value = "oid") String oid,HttpServletRequest request) throws Exception {
        UserBankCardEntity userBankCardEntity = userBankCardDao.selectOne(new QueryWrapper<UserBankCardEntity>()
                .eq("oid", oid).eq("is_delete", 0));
        UserBankCardDto userBankCardDto= BeanUtilsMabach.doToDto(userBankCardEntity, UserBankCardDto.class);
        if(userBankCardDto!=null){
            EmployeeInfoEntity employeeInfoEntity= employeeInfoService.getByIdE(Long.valueOf(oid));
            userBankCardDto.setName(employeeInfoEntity.getName());
            List<AuthorityLogEntity> authorityLogEntityList=authorityLogDao.selectList(new QueryWrapper<AuthorityLogEntity>()
                    .eq("oid",oid)
                    .eq("is_delete", 0)
                    .eq("type", 3)
            );
            List<UserBankCardAnnalDto> UserBankCardAnnalDtoList=new ArrayList<>();
            if( !CollectionUtils.isEmpty(authorityLogEntityList)){
                for (AuthorityLogEntity authorityLogEntity:authorityLogEntityList){
                    UserBankCardEntity userBankCard= JSONObject.parseObject(authorityLogEntity.getParams(), UserBankCardEntity.class);
                    UserBankCardAnnalDto userBankCardAnnalDto= BeanUtilsMabach.doToDto(userBankCard, UserBankCardAnnalDto.class);
                    userBankCardAnnalDto.setInvalidDate(authorityLogEntity.getUpdateTime());
                    UserBankCardAnnalDtoList.add(userBankCardAnnalDto);
                }
            }
            userBankCardDto.setUserBankCardAnnalDtoList(UserBankCardAnnalDtoList);
        }
        return R.ok(userBankCardDto);
    }


    @GetMapping("/getCredentialList")
    @ApiOperation("员工资质列表")
    public R<PageResult<CredentialInfoDto>> getCredentialList(@RequestParam(value = "oid") String oid,
                                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                              HttpServletRequest request)throws Exception{
        PageResult<CredentialInfoDto> result = credentialInfoService.getCredentialList(Long.valueOf(oid),pageNum,pageSize);
        List<CredentialInfoDto> list = result.getList();
        if(!CollectionUtils.isEmpty(list)){
            for(CredentialInfoDto dto:list){
                dto.setJobName(jobController.jobMap.get(dto.getJobId()));
                dto.setOrgName(jobController.orgMap.get(dto.getOrgId()));
                dto.setUnitName(jobController.orgMap.get(dto.getUnitId1()));
            }
        }
        return R.ok(result);
    }


    @GetMapping("/getAwardInfoList")
    @ApiOperation("查询员工奖罚信息")
    public R getAwardInfoList(@RequestParam(value = "oid") String oid,
                              @RequestParam(value = "dateStart" ,required = false) String dateStart,
                              @RequestParam(value = "dateEnd",required = false) String dateEnd,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                              HttpServletRequest request)throws Exception{
        Page<AwardInfoAndApplicantDto> page = new Page<AwardInfoAndApplicantDto>(pageNum,pageSize);
        List<AwardInfoAndApplicantDto> awardInfoAndApplicantDtoList=  awardInfoDao.getAwardInfoPage(oid,dateStart,dateEnd,page);

        page.setRecords(awardInfoAndApplicantDtoList);
        return R.ok(new PageResult<AwardInfoAndApplicantDto>(page));
    }


    @GetMapping("/getSalaryChangeList")
    @ApiOperation("查询员工薪资调整信息")
    public R getSalaryChangeList(@RequestParam(value = "oid") String oid,
                                 @RequestParam(value = "dateStart",required = false) String dateStart,
                                 @RequestParam(value = "dateEnd",required = false) String dateEnd,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                              HttpServletRequest request)throws Exception{
        Page<SalaryChangeDto> page = new Page<SalaryChangeDto>(pageNum,pageSize);
        List<SalaryChangeDto> salaryChangeList=  salaryChangeDao.getSalaryChangePage(oid,dateStart,dateEnd,page);
        for (SalaryChangeDto salaryChangeDto : salaryChangeList) {
            if (salaryChangeDto.getJobId() != null && StringUtils.isEmpty(salaryChangeDto.getJobname()))
                salaryChangeDto.setJobname(jobController.getJobName(salaryChangeDto.getJobId()));
            if (salaryChangeDto.getOrgId() != null && StringUtils.isEmpty(salaryChangeDto.getOrgname()))
                salaryChangeDto.setOrgname(jobController.getOrgNameById(salaryChangeDto.getOrgId()));
            if (salaryChangeDto.getUnitId() != null && StringUtils.isEmpty(salaryChangeDto.getUnitname()))
                salaryChangeDto.setUnitname(jobController.getOrgNameById(salaryChangeDto.getUnitId()));

            if (salaryChangeDto.getNewJobId() != null && StringUtils.isEmpty(salaryChangeDto.getNewJobName()))
                salaryChangeDto.setNewJobName(jobController.getJobName(salaryChangeDto.getNewJobId()));
            if (salaryChangeDto.getNewOrgId() != null && StringUtils.isEmpty(salaryChangeDto.getNewOrgName()))
                salaryChangeDto.setNewOrgName(jobController.getOrgNameById(salaryChangeDto.getNewOrgId()));
            if (salaryChangeDto.getNewUnitId() != null && StringUtils.isEmpty(salaryChangeDto.getNewUnitName()))
                salaryChangeDto.setNewUnitName(jobController.getOrgNameById(salaryChangeDto.getNewUnitId()));
        }
        page.setRecords(salaryChangeList);
        return R.ok(new PageResult<SalaryChangeDto>(page));
    }


    @GetMapping("/getPersonSecurityList")
    @ApiOperation("获取个人社保记录")
    public R<PageResult<SecurityRecordDto>> getPersonSecurityList(
            @RequestParam(value = "oid") String oid,
            @RequestParam(value = "dateStart",required = false) String dateStart,
            @RequestParam(value = "dateEnd",required = false) String dateEnd,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest request)throws Exception{
        PageResult<SecurityRecordDto> recordMap = securityRecordService.getRecordList(dateStart, dateEnd,null, 1, null,null,
                null,oid,null,null,null,pageNum, pageSize);
        return R.ok(recordMap);
    }

    @GetMapping("/getPersonFundsList")
    @ApiOperation("获取个人公积金记录")
    public R<PageResult<FundsRecordDto>> getPersonFundsList(
            @RequestParam(value = "oid") String oid,
            @RequestParam(value = "dateStart",required = false) String dateStart,
            @RequestParam(value = "dateEnd",required = false) String dateEnd,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize)throws Exception{
        PageResult<FundsRecordDto> recordMap = fundsRecordService.getRecordList(dateStart,dateEnd, null, 1, null,null,
                null,oid,null,null, pageNum, pageSize);
        return R.ok(recordMap);
    }

    /***
     *   <Step title="新建" description="2020-10-18" />
     *             <Step title="录用审批" description="2020-11-06" />
     *             <Step title="Offer发送" description="2020-11-06"/>
     *             <Step title="信息采集" description="2020-11-06"/>
     *             <Step title="确认入职"/>
     *             <Step title="合同签订"/>
     * @param oid
     * @return
     */
    @GetMapping("/getRecruitment")
    @ApiOperation("获取个人招聘录用信息")
    public R getRecruitment(@RequestParam(value = "oid") String oid){
        JSONArray jsonArray=new JSONArray();
        EmployeeInfoEntity employeeInfoEntity= employeeInfoService.getByIdE(Long.valueOf(oid));
        JSONObject obj1=new JSONObject();
        obj1.put("title","新建");
        obj1.put("day", DateUtils.format(employeeInfoEntity.getCreateTime()));
        jsonArray.add(obj1);

        EmployeeBpmEntity employeeBpmEntity= employeeBpmService.getByIdE(Long.valueOf(oid));
        JSONObject obj2=new JSONObject();
        obj2.put("title","发起录用");
        if(ObjectUtils.isEmpty(employeeBpmEntity)){
            obj2.put("day", null);
        }else{
            obj2.put("day", DateUtils.format(employeeBpmEntity.getCreateTime()));
        }
        jsonArray.add(obj2);

        List<MailRecordEntity> mailRecordEntityList= mailRecordDao.selectList(new QueryWrapper<MailRecordEntity>()
                .eq("oid",oid)
                .eq("is_delete",0).orderByDesc("create_time"));
        JSONObject obj3=new JSONObject();
        obj3.put("title","Offer发送");
        if(CollectionUtils.isEmpty(mailRecordEntityList)){
            obj3.put("day", null);
        }else {
            obj3.put("day", DateUtils.format(mailRecordEntityList.get(0).getCreateTime()));
        }
        jsonArray.add(obj3);

      //  EmployeeBpmEntity employeeBpmEntity= employeeBpmService.getByIdE(Long.valueOf(oid));
        JSONObject obj4=new JSONObject();
        obj4.put("title","信息采集");
        obj4.put("day",null);
        jsonArray.add(obj4);

        JSONObject obj5=new JSONObject();
        obj5.put("title","确认入职");
        obj5.put("day", DateUtils.format(employeeInfoEntity.getHiredate()));
        jsonArray.add(obj5);


        List<ContractMangerEntity> list =new ArrayList<>();
        if(!CollectionUtils.isEmpty(mailRecordEntityList)){
             list  = contractMangerDao.selectList(new QueryWrapper<ContractMangerEntity>()
                .eq("type", ContractConst.CONTRACT_TYPE_NEW).eq("oid",oid)
                .eq("is_delete",0)
                .ge("create_time",DateUtils.format(mailRecordEntityList.get(0).getCreateTime())+" 00:00:00")
                .orderByDesc("create_time"));
        }
        JSONObject obj6=new JSONObject();
        obj6.put("title","合同签订");
        if(CollectionUtils.isEmpty(list)){
            obj6.put("day", null);
        }else {
            obj6.put("day", DateUtils.format(list.get(0).getSignDate()));
        }
        jsonArray.add(obj6);

        return R.ok(jsonArray);
    }

    @PostMapping("/getAttendMonth")
    @ApiOperation("获取月出勤")
    public R<AttendMemberDto> getAttendMonth(@RequestBody @Valid AttendVo vo,HttpServletRequest request)throws Exception{
        JSONObject userInfo = loginController.getUserInfo(request).getData();
        String name = userInfo.getString("user_name");
        Long orgId = userInfo.getLong("deptId");
        String startTime = DateUtils.format(vo.getStartDate())+" 00:00:00";
        String endTime = DateUtils.format(vo.getEndDate())+" 23:59:59";

        AttendMonthDto dto =  new AttendMonthDto();
//                dto  =jobDaoMssql.getMonthAttend(vo.getCode(),startTime,endTime);
        Date date = new Date();
        String dateStr = DateUtils.format(date,"yyyy-MM")+"-01";
        String endDate =DateUtils.format(DateUtils.addDateMonths(date,1),"yyyy-MM")+"-01" ;
        if(!ObjectUtils.isEmpty(vo.getStartDate())){
            dateStr = DateUtils.format(vo.getStartDate(),"yyyy-MM-dd");
        }
        if(!ObjectUtils.isEmpty(vo.getEndDate())){
            endDate =DateUtils.format(vo.getEndDate(),"yyyy-MM-dd");
        }
//        List<AttendDto> attendDtoList = jobDaoMssql.getAttendDtoList(dateStr+" 00:00:00",endDate+" 04:00:00",orgId,vo.getCode());
        List<AttendDto> attendDtoList = new ArrayList<>();
        AttendMemberDto memberDto = new AttendMemberDto();
        memberDto.setAttendMonthDto(dto);
        memberDto.setDtoList(attendDtoList);
        return R.ok(memberDto);
    }

    
    private void setParam(EmployeeInfoEntity employeeInfoEntity, EmployeeInfoDto employeeInfoDto) {
        /**
         * 根据各种id查询名称
         */
        Long unitId = employeeInfoEntity.getUnitId();
        Long orgId = employeeInfoEntity.getOrgId();
        Long jobId = employeeInfoEntity.getJobId();

        Long jobSystemId = employeeInfoEntity.getJobSystemId();
        Long lineId = employeeInfoEntity.getLineId();


        Long jobLevelId = employeeInfoEntity.getJobLevelId();
        String jobLevelNameById = jobController.getJobLevelNameById(jobLevelId);
        employeeInfoDto.setJobLevel(jobLevelNameById);


        String systemNameById = jobController.getSystemNameById(jobSystemId);
//        log.info("OrgSystemEntity:{}",jobLevelById);

        employeeInfoDto.setJobSystem(systemNameById);

        String lineNameById = jobController.getLineNameById(lineId);

        employeeInfoDto.setLine(lineNameById);


        String org = jobController.getOrgNameById(orgId);
        employeeInfoDto.setOrgname(org);


        String unit = jobController.getOrgNameById(unitId);
        employeeInfoDto.setUnitname(unit);


        String jobName = jobController.getJobName(jobId);
        employeeInfoDto.setJobname(jobName);
    }

    private void getImg(Long oid, EmployeeInfoDto employeeInfoDto) {
        //        如果薪资流水不为空，则封装进去
        List<ImgUrlEntity> imgUrlEntities = imgUrlDao.selectList(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 1));
        if (!CollectionUtils.isEmpty(imgUrlEntities)){
            ArrayList<String> recordUrls = new ArrayList<>();
            for (ImgUrlEntity imgUrlEntity : imgUrlEntities) {
                recordUrls.add(imgUrlEntity.getUrl());
            }
            employeeInfoDto.setOldSalaryRecordUrl(recordUrls);
        }
//        封装头像
        ImgUrlEntity imgUrlEntity = imgUrlDao.selectOne(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 0));
        if (!ObjectUtils.isEmpty(imgUrlEntity)){
            employeeInfoDto.setPhotoUrl(imgUrlEntity.getUrl());
        }

        //0:流水，1：头像，2：毕业证,3:学位证，4：离职证明,5：体检报告
        ImgUrlEntity imgUrlEntity2 = imgUrlDao.selectOne(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 2));
        if (!ObjectUtils.isEmpty(imgUrlEntity2)){
            employeeInfoDto.setGraduationUrl(imgUrlEntity2.getUrl());
        }
//        学位证
        ImgUrlEntity imgUrlEntity3 = imgUrlDao.selectOne(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 3));
        if (!ObjectUtils.isEmpty(imgUrlEntity3)){
            employeeInfoDto.setDegreeUrl(imgUrlEntity3.getUrl());
        }
//        离职证明
        ImgUrlEntity imgUrlEntity4 = imgUrlDao.selectOne(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 4));
        if (!ObjectUtils.isEmpty(imgUrlEntity4)){
            employeeInfoDto.setDimissionUrl(imgUrlEntity4.getUrl());
        }
//        体检报告
        ImgUrlEntity imgUrlEntity5 = imgUrlDao.selectOne(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 5));
        if (!ObjectUtils.isEmpty(imgUrlEntity5)){
            employeeInfoDto.setHealthUrl(imgUrlEntity5.getUrl());
        }

        //        身份证
        List<ImgUrlEntity> imgUrlEntityList = imgUrlDao.selectList(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 6));
        ArrayList<String> strings = new ArrayList<>();
        if (!CollectionUtils.isEmpty(imgUrlEntityList)){
            for (ImgUrlEntity urlEntity : imgUrlEntityList) {
                strings.add(urlEntity.getUrl());
            }
            employeeInfoDto.setIdcardImgs(strings);
        }

//职称
        List<ImgUrlEntity> imgUrlEntityList3 = imgUrlDao.selectList(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 10));
        ArrayList<String> strings1 = new ArrayList<>();
        if (!CollectionUtils.isEmpty(imgUrlEntityList3)){
            for (ImgUrlEntity urlEntity : imgUrlEntityList3) {
                strings1.add(urlEntity.getUrl());
            }
            employeeInfoDto.setCredential(strings1);
        }

//        其他附件

        List<ImgUrlEntity> imgUrlEntityList4 = imgUrlDao.selectList(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 11));
        ArrayList<String> strings2 = new ArrayList<>();
        if (!CollectionUtils.isEmpty(imgUrlEntityList4)){
            for (ImgUrlEntity urlEntity : imgUrlEntityList4) {
                strings2.add(urlEntity.getUrl());
            }
            employeeInfoDto.setAttachments(strings2);
        }

    }

    }





